<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'type' => 'in:1,2,3,4,5',
            'keyword' => 'string|max:60',
            'category_id' => 'integer',
            'circle_id' => 'integer'
        ]);

        $query = Article::query()
                    ->where('user_id',auth('api')->id());
        if (isset($param['type']) && $param['type']!==''){
            $query->where('type',$param['type']);
        }

        if (isset($param['category_id']) && $param['category_id']!==''){
            $query->where('category',$param['category_id']);
        }


        if (isset($param['keyword']) && $param['keyword']!==''){
            $query->where('keyword','like','%'.$param['keyword'].'%');
        }

        if (isset($param['circle_id']) && $param['circle_id'] !==''){
            $query->where('join_id',$param['circle_id']);
        }


        $paginate =  $query->orderBy('sort')
            ->where('audit_status',2)
            ->where('status',1)
            ->orderByDesc('id')->simplePaginate();


        return $this->success($paginate);

    }

}
